﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using Osobe;
using SkolskaUstanova;
using MolbeIPotvrde;

namespace ElektronskiDnevnik
{
    public partial class IRoditelj : Form
    {

        MySqlDataReader rdr = null;
        MySqlConnection conn = new MySqlConnection("SERVER=etf.ba;" + "DATABASE=ooadtim8;" + "UID=ooadtim8;" + "PASSWORD=ooadtim8;");
        Prijavljivanje stara = new Prijavljivanje();
        String JMBG;
        Roditelj roditelj;
        String odabranoDijete;
        public IRoditelj(Prijavljivanje f, String maticni)
        {
            InitializeComponent();
            stara = f;
            JMBG = maticni;
            MySqlCommand cmd = new MySqlCommand("SELECT * FROM Roditelj WHERE JMBG = '" + JMBG + "';", conn);
            try
            {
                conn.Open();
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    roditelj = new Roditelj(Convert.ToString(rdr["Ime"]), Convert.ToString(rdr["Prezime"]), Convert.ToString(rdr["JMBG"]), Convert.ToDateTime(rdr["DatumRodjenja"]), Convert.ToString(rdr["MjestoRodjenja"]), Convert.ToString(rdr["Ulica"]) + ' ' + Convert.ToString(rdr["Broj"]), Convert.ToString(rdr["Password"]), Convert.ToString(rdr["Zanimanje"]));
                    pictureBox1.ImageLocation = Convert.ToString(rdr["URLSlike"]);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (rdr != null)
                    rdr.Close();
            }
        }

        private void Form3_Paint(object sender, PaintEventArgs e)
        {
            Font font = new Font("Segoe Print", 28, FontStyle.Regular);
            SolidBrush brush = new SolidBrush(Color.Salmon);
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            e.Graphics.DrawString("Dobrodošli, " + roditelj.Ime ,font, brush, 170, 30);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            flowLayoutPanel1.Visible = false;
            panel1.Visible = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
           PregledUspjehaU nova = new PregledUspjehaU(odabranoDijete, conn);
           nova.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            PregledPrisustvaPravdanjeR nova = new PregledPrisustvaPravdanjeR(odabranoDijete, conn);
            nova.Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            PodnosenjeZahtjeva nova = new PodnosenjeZahtjeva(JMBG, odabranoDijete,conn);
            nova.Show();
        }

        private void b_Click(object sender, EventArgs e)
        {
            flowLayoutPanel1.Visible = false;
            panel1.Visible = true;
            Button b = new Button();
            b = (Button)sender;
            String select = b.Text;
            odabranoDijete = select.Substring(0,13);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            var rez = MessageBox.Show("Sigurni ste da se zelite odjaviti?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (rez == DialogResult.No) return;
            else
            {
                this.Close();
                stara.Show();
            }
        }

        private void Form3_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (conn != null)
                conn.Close();
            stara.Show();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            PregledZahtjeva nova = new PregledZahtjeva(JMBG, odabranoDijete, conn);
            nova.Show();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            List<String> ur = new List<String>();
            textBox1.Text = roditelj.Ime;
            textBox2.Text = roditelj.Prezime;
            textBox3.Text = roditelj.Jmbg;
            textBox4.Text = Convert.ToString((roditelj.DatumRodjenja).Day + "/" + (roditelj.DatumRodjenja).Month + "/" + (roditelj.DatumRodjenja).Year);
            textBox5.Text = roditelj.MjestoRodjenja;
            textBox6.Text = roditelj.Adresa;
            textBox7.Text = roditelj.Zanimanje;
            MySqlDataReader reader = null;
            MySqlCommand cmd = new MySqlCommand("SELECT * FROM UcenikRoditelj WHERE Roditelj = " + JMBG + ";", conn);
            try
            {
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    String uc = Convert.ToString(reader["Ucenik"]);
                    ur.Add(uc);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }
            for (int i = 0; i < ur.Count(); i++)
            {
                MySqlDataReader rdr = null;
                MySqlCommand cmd1 = new MySqlCommand("SELECT * FROM Ucenik WHERE JMBG = '" + ur[i] + "';", conn);
                try
                {
                    rdr = cmd1.ExecuteReader();
                    while (rdr.Read())
                    {
                        Button b = new Button();
                        b.Text = Convert.ToString(rdr["JMBG"] + "\n" + rdr["Ime"]);
                        b.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
                        b.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
                        b.ForeColor = System.Drawing.Color.Salmon;
                        flowLayoutPanel1.Controls.Add(b);
                        b.Size = new System.Drawing.Size(130, 50);
                        b.TabIndex = i+1;
                        b.Click += new System.EventHandler(this.b_Click);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    if (rdr != null)
                        rdr.Close();
                }
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
            flowLayoutPanel1.Visible = true;    
        }

        private void button4_Click(object sender, EventArgs e)
        {
            PromjenaPassworda nova = new PromjenaPassworda(roditelj.Jmbg, conn, "2");
            nova.Show();
        } 
    }
}
